<template>
  <div>
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin: 10px 0px"
      @click="handleAdd"
    >添加</el-button>
    <div class="table">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="序号" align="center" width="80px">
          <template v-slot="scope">
            {{
              (pagination.currentPage - 1) * pagination.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column label="品牌名称" prop="tmName" />
        <el-table-column label="品牌logo" prop="logoUrl">
          <template v-slot="scope">
            <img
              :src="scope.row.logoUrl"
              alt=""
              style="width: 40px; height: 40px"
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            >修改</el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="handleDel(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination" style="margin-top: 20px; text-align: center">
      <el-pagination
        :total="pagination.total"
        :page-size="pagination.pageSize"
        :page-sizes="[1, 3, 5, 10]"
        :current-page="pagination.currentPage"
        layout="prev,pager,next,jumper,->,sizes,total"
        @current-change="handleCurChange"
        @size-change="handleSizeChange"
      />
    </div>
    <component
      :is="'Dialog'"
      ref="Dialog"
      :value="showDialog"
      :pagination="pagination"
      @handleClose="handleClose"
      @handleCurChange="handleCurChange"
    />
  </div>
</template>

<script>
import { getTradeMarkList, delTradeMark } from '@/api/product/tradeMark'
import pagination from '@/mixins/pagination'
export default {
  name: 'TradeMark',
  components: {
    Dialog: () => import('./dialog.vue')
  },
  mixins: [pagination],
  data() {
    return {
      tableData: [],
      showDialog: false
      // pagination: {
      //   total: 100,
      //   pageSize: 3,
      //   currentPage: 1,
      // }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取品牌列表
    async getData() {
      const { pagination } = this
      const res = await getTradeMarkList(
        pagination.currentPage,
        pagination.pageSize
      )
      // console.log('获取品牌列表res:', res)
      // 列表数据
      this.tableData = res.records
      // 总条数
      pagination.total = res.total
    },
    // 添加品牌弹框
    handleAdd() {
      this.showDialog = true
    },
    // 关闭弹框
    handleClose() {
      this.showDialog = false
    },
    // 编辑品牌
    handleEdit(row) {
      this.showDialog = true
      this.$refs.Dialog.mode = 'edit'
      this.$refs.Dialog.form = JSON.parse(JSON.stringify(row))
    },
    // 删除品牌
    handleDel(row) {
      this.$confirm('您确定要删除该品牌吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delTradeMark(row.id)
          this.$message.success('删除品牌成功')
          this.handleCurChange(1)
        })
        .catch(() => {})
    }
  }
}
</script>

<style></style>
